package com.yuhao.server.controller;

import com.yuhao.common.entity.serviceb.ServiceBOutput;
import com.yuhao.server.entity.ServiceBTest;
import com.yuhao.server.service.ServiceBTestService;
import com.yuhao.server.util.BaseController;
import com.yuhao.server.util.ResultData;
import com.yuhao.server.util.ResultType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/serviceb")
@Api(value = "serviceb test")
public class ServiceBController extends BaseController {

    @Autowired
    private ServiceBTestService service;


    @PostMapping("/addTest")
    @ApiOperation( value = "添加测试记录 ", notes = "传入ServiceATest")
    @ApiResponse(
            code = 200,
            message = "code == 200 成功",
            response = ResultData.class
    )
    public ResultData addTest(ServiceBTest aTest) {
        service.addTest(aTest);
        return setResponseEntity(ResultType.SUCCESS.getDescription(), ResultType.SUCCESS.getCode(), "操作成功", true);
    }

    @GetMapping("/findAllTest")
    @ApiOperation( value = "查找所有test ", notes = "")
    @ApiResponse(
            code = 200,
            message = "code == 200 成功",
            response = ResultData.class
    )
    public ResultData findAllTest() {
        List<ServiceBTest> bTests =  service.findAllTest();
        return setResponseEntity(ResultType.SUCCESS.getDescription(), ResultType.SUCCESS.getCode(), bTests, true);
    }

    /** 远程服务给servicea调用 */
    @GetMapping("/feignFindAllTest")
    @ApiOperation( value = "给远程服务查找所有test ", notes = "")
    @ApiResponse(
            code = 200,
            message = "code == 200 成功",
            response = ResultData.class
    )
    public List<ServiceBOutput> feignfindAllTest() {
        List<ServiceBOutput> outputs =  service.findAllTest().stream().map(
                e -> {
                    ServiceBOutput output = new ServiceBOutput();
                    BeanUtils.copyProperties(e, output);
                    return output;
                }).collect(Collectors.toList());;
        return outputs;
    }
}
